﻿/// <reference path="jquery.js" />
/// <reference path="jquery.form-extensions.js" />
(function($) {
    $.fn.datetimepicker = function(options) {
//        if (arguments.length > 2) {
//            if (arguments[1] == "minDate")
//                this.data('mintime', new Date(arguments[2]));
//            else if (arguments[1] == "maxDate")
//                this.data('maxtime', new Date(arguments[2]));
//        }
        if (typeof options == typeof '') return this.datepicker.apply(this, arguments);
        options = $.extend({}, options);
        options.showTime = true;
        options.constrainInput = false;

        return this.datepicker(options);
    }

    $.datepicker._getTimeText = function(inst, h, m) {
        h = h || inst.selectedHour || 0;
        m = m || inst.selectedMinute || 0;

        if (this._get(inst, 'clockType') == 12) {
            return (h == 0 || h == 12 ? '12' : h >= 12 ? h - 12 : h)
            + ':' + (m < 10 ? '0' + m : m)
            + ' ' + (h < 12 ? 'AM' : 'PM');
        } else return h + ':' + (m < 10 ? '0' + m : m);
    }

    var formatDate = $.datepicker.formatDate;
    $.datepicker.formatDate = function(format, date, settings) {
        //console.log(this);
        var inst = $.datepicker._curInst;
        var showTime = inst ? this._get(inst, 'showTime') : false;
        if (showTime) {
            date.setHours(inst.selectedHour || 0);
            date.setMinutes(inst.selectedMinute || 0);
        }

        return formatDate.apply(this, arguments) + (showTime ? ' ' + this._getTimeText(inst) : '');
    }
    $.datepicker._defaults.clockType = 12;

    var _hideDatepicker = $.datepicker._hideDatepicker;
    $.datepicker._hideDatepicker = /*overrides*/function() {
        var inst = $.datepicker._curInst;
        inst.selectedHour = inst.currentHour = inst.selectedMinute = inst.currentMinute = undefined;
        _hideDatepicker.apply(this, arguments);
    }

    var _getDate = $.datepicker._getDate;
    $.datepicker._getDate = /*overrides*/function(inst) {
        var date = _getDate.apply(this, arguments);
        if (date) {
            date.setHours(inst.selectedHour || inst.currentHour);
            date.setMinutes(inst.selectedMinute || inst.currentMinute);
        }

        return date;
    }

    var parseDate = $.datepicker.parseDate;
    var rxTime = /\s+([0-9]+)(\:[0-9]+){0,1}(\:[0-9]+){0,1}(\s*[apm]+){0,1}/i;
    $.datepicker.parseDate = function(format, value, settings) {
        var h = 0, m = 0;

        var time = value.match(rxTime);
        if ($.isArray(time)) {
            h = parseFloat(time[1]);
            m = parseFloat((time[2] || '').replace(':', ''));
            if (isNaN(h)) h = 0;
            if (isNaN(m)) m = 0;

            if ((settings.clockType || $.datepicker._defaults.clockType) == 12) {
                if (h == 12) h = 0;
                if (time[0].toLowerCase().indexOf('p') > -1) h += 12;
            }

            value = value.replace(time[0], '');
        }

        var val = parseDate.apply(this, arguments);
        if (val) {
            val.setHours(h);
            val.setMinutes(m);
        }

        return val;
    }

    $.datepicker._setDateFromField = /*overrides*/function(inst, noDefault) {
        if (inst.input.val() == inst.lastVal) {
            return;
        }
        var dateFormat = this._get(inst, 'dateFormat');
        var dates = inst.lastVal = inst.input ? inst.input.val() : null;
        var date, defaultDate;
        date = defaultDate = this._getDefaultDate(inst);
        var settings = this._getFormatConfig(inst);
        try {
            date = this.parseDate(dateFormat, dates, settings) || defaultDate;
        } catch (event) {
            this.log(event);
            dates = (noDefault ? '' : dates);
        }
        inst.selectedDay = date.getDate();
        inst.drawMonth = inst.selectedMonth = date.getMonth();
        inst.drawYear = inst.selectedYear = date.getFullYear();
        inst.currentDay = (dates ? date.getDate() : 0);
        inst.currentMonth = (dates ? date.getMonth() : 0);
        inst.currentYear = (dates ? date.getFullYear() : 0);
        inst.currentHour = (dates ? date.getHours() : 0);
        inst.currentMinute = (dates ? date.getMinutes() : 0);
        this._adjustInstDate(inst);
    }

    var _updateDatepicker = $.datepicker._updateDatepicker;
    $.datepicker._updateDatepicker = /*overrides*/function(inst) {
        var showTime = this._get(inst, 'showTime');
        var buttons = this._get(inst, 'buttons');
//        var mindate = inst.input.data('mintime') || new Date(1000, 1, 1, 0, 0, 0);
//        var maxdate = inst.input.data("maxtime") || new Date(9999, 1, 1, 23, 59, 0);




        var self = this;

        if (buttons) inst.settings['showButtonPanel'] = true;

        _updateDatepicker.apply(this, arguments);
        var zIndex = inst.dpDiv.zIndex();
        zIndex = zIndex > 1000 ? zIndex : 1000;
        inst.dpDiv.css({ zIndex: zIndex });
        if (buttons) {
            var panel = inst.dpDiv.find('.ui-datepicker-buttonpane:first').html('');

            for (var i in buttons) {
                var button = $('<button class="ui-datepicker-current ui-state-default ui-priority-secondary ui-corner-all"/>');

                (function(func) {
                    if (typeof func == 'object') {
                        if (func.attr)
                            button.attr(func.attr);
                        if (func.css)
                            button.css(func.css);
                        func = func.click || function() { };
                    }
                    button.text(i).click(function() { func.apply(self, ['#' + inst.id, inst]); }).appendTo(panel);
                })(buttons[i]);
            }
        }
        var checktime = function(inst) {
//            var date = $.datepicker._getDate(inst);
//            if (date) {
//                if (date <= mindate) {
//                    date = new Date(mindate);
//                    date.setMinutes(date.getMinutes() + 1);

//                } else if (date >= maxdate) {
//                    date = new Date(maxdate);
//                    date.setMinutes(date.getMinutes() - 1);
//                }
//                inst.selectedHour = date.getHours();
//                inst.selectedMinute = date.getMinutes();
//            }




        };
        if (showTime) {
            var table = inst.dpDiv.find('table:last');
            var thead = table.find('thead>tr:first');
            var tbody = table.find('tbody>tr:first');
            var numRows = tbody.parent().children('tr').length;

            var lblTime = $('<th colspan="2" style="white-space:nowrap">12:00 AM</th>').appendTo(thead);
            lblTime.width(lblTime.width());
            var dpwidth = inst.dpDiv.width();
            inst.dpDiv.width(dpwidth + lblTime.width());
            var groups = inst.dpDiv.find('.ui-datepicker-group');
            groups.width(dpwidth / groups.length);
            groups.eq(groups.length - 1).width(dpwidth / groups.length + lblTime.width());

            var height = table.height() - table.find('td:first').height() * 2;
            var tdHour = $('<td/>').css({ height: height, marginTop: 10, paddingLeft: 20 }).attr('rowspan', numRows).appendTo(tbody);
            var tdMin = $('<td/>').css({ height: height, marginTop: 10 }).attr('rowspan', numRows).appendTo(tbody);

            var divHour = $('<div/>').appendTo(tdHour);
            var divMin = $('<div/>').appendTo(tdMin);

            inst.selectedHour = inst.selectedHour || inst.currentHour;
            inst.selectedMinute = inst.selectedMinute || inst.currentMinute;
//            checktime(inst);

            lblTime.text($.datepicker._getTimeText(inst));

            divHour.slider({ min: 0, max: 23, value: inst.selectedHour, orientation: 'vertical', slide: function(e, ui) {
                inst.selectedHour = ui.value;
                //checktime(inst);
                if (inst.input)
                    inst.input.val($.datepicker._formatDate(inst));
                lblTime.text($.datepicker._getTimeText(inst));
            }
            });
            divMin.slider({ min: 0, max: 59, value: inst.selectedMinute, orientation: 'vertical', slide: function(e, ui) {
                inst.selectedMinute = ui.value;
               // checktime(inst);
                if (inst.input)
                    inst.input.val($.datepicker._formatDate(inst));
                lblTime.text($.datepicker._getTimeText(inst));
            }
            });
        }
    }
})(jQuery);


